package com.crm.service.impl.index;

import com.crm.common.model.Enum.StatusCodeEnum;
import com.crm.dao.mapper.sys.FileUploadMapper;
import com.crm.dao.mapper.sys.NewsMapper;
import com.crm.dao.mapper.sys.NotifyMapper;
import com.crm.model.entity.sys.Notify;
import com.crm.model.entity.sys.NotifyExample;
import com.crm.service.index.AnnouncementDbService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;

/**
 * @author wll
 * @date 2020/12/18 15:18
 */
@Service
public class AnnouncementDbServiceImpl implements AnnouncementDbService {
    @Autowired
    FileUploadMapper fileUploadMapper;

    @Autowired
    NotifyMapper notifyMapper;

    @Autowired
    NewsMapper newsMapper;


    /**
     * @param searchInfo   搜索条件
     * @param publicStatus 发布状态 0:已发布; 1:已删除',
     * @return
     * @author wll
     * @date 2020/12/21 08:50
     */
    @Override
    public List<Notify> selectAnnouncementBySearchInfo(String searchInfo, Integer publicStatus,String sort,String sortOrder) {
        NotifyExample notifyExample = new NotifyExample();
        NotifyExample.Criteria criteria = notifyExample.createCriteria();
        criteria.andTypeEqualTo(1);

        if (StringUtils.isNotEmpty(searchInfo)) {
            criteria.andTitleLike("%" + searchInfo.trim() + "%");
        }

        if (null == publicStatus || publicStatus == 0) {//已发布
            criteria.andDeleteStatusEqualTo(Short.parseShort(String.valueOf(StatusCodeEnum.NORMAL_CODE.getState())));
        } else {//已删除
            criteria.andDeleteStatusEqualTo(Short.parseShort(String.valueOf(StatusCodeEnum.DELETE_CODE.getState())));
        }

        //拼凑表格排序内容
        String order = "is_top desc";
        if(!StringUtils.isBlank(sort)&&!StringUtils.isBlank(sortOrder)){
            switch(sort){
                case "createTm":
                {
                    order= order + ",create_tm " + sortOrder;
                }
                break;
                case "updateTm":
                {
                    order= order + ",update_tm " + sortOrder;
                }
                break;
                default:
                    break;
            }

        }
        notifyExample.setOrderByClause(order);
        List<Notify> notifies = notifyMapper.selectByExampleWithBLOBs(notifyExample);
        return notifies;
    }



    @Override
    public Boolean insertAnnouncement(Notify notify) {
        Boolean result = false;
        int num = notifyMapper.insertSelective(notify);
        if (num > 0) {
            result = true;
        }
        return result;
    }



    @Override
    public Boolean updateAnnouncementByPrimaryKey(Notify notify) {
        Boolean result = false;
        int num = notifyMapper.updateByPrimaryKeySelective(notify);
        if (num > 0) {
            result = true;
        }
        return result;
    }

    @Override
    public Notify selectAnnouncementById(String id) {
        Notify notify = notifyMapper.selectByPrimaryKey(id);
        return notify;
    }
}
